<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use App\Model\SidebarModel;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /** @var array 入参校验后的参数 */
    static public $params = [];
    /** @var array 渲染的数据 */
    protected $data = [];

    public function __construct()
    {
        list($controller, $action) = explode('@', request()->route()->getActionName());
        $this->data['identity'] = strtolower(str_replace(['App\Http\Controllers\\', 'Controller', '\\'], ['', '', '/'], $controller) . '/' . $action);
    }

    /** 渲染侧边栏 */
    public function showSidebar()
    {
        $backendUser = Auth::user();
        if ($backendUser->backend_group_id) {
            $this->data['sidebar'] = SidebarModel::listGroupSidebar($backendUser->is_group_admin == 1);
        } else {
            $this->data['sidebar'] = SidebarModel::listSystemSidebar();
        }
    }

    /**
     * 请求成功-响应输出
     * @param string $message
     * @param array $data
     * @return false|string
     */
    protected function responseSuccess( $message = '请求成功', array $data = [])
    {
        return json_encode(['data' => $data, 'message' => $message, 'code' => 200]);
    }

    /**
     * 请求失败-响应输出
     * @param string $message
     * @param array $data
     * @return false|string
     */
    protected function responseError($message = '请求失败成功', array $data = [])
    {
        return json_encode(['data' => $data, 'message' => $message, 'code' => 500]);
    }
}
